package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.data.RoomContactMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RoomContact;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 设置管理员回调
 *
 * <pre>
 * {
 *     "nType": 4509,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": [
 *         {
 *             "nType": 11, // 设置管理员类型 10 增加管理员 11移除管理员
 *             "vcChatRoomId": "",
 *             "vcChatRoomSerialNo": "",
 *             "vcManagerWxId": "QQ603270540",
 *             "vcManagerSerialNo": "",
 *             "nIfRobot": 1    // 该管理员是否为机器人 0 机器人 1 非机器人
 *         }
 *     ]
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4509Handler implements CallbackHandler {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private RoomContactMapper roomContactMapper;

    @Override
    public String name() {
        return "机器人设置群管理员回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4509;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONArray data = (JSONArray) callback.getData();
        for (Object o : data) {
            JSONObject datum = JSONObject.from(o);
            String roomId = datum.getString("vcChatRoomId");
            String contactId = datum.getString("vcManagerWxId");
            boolean isManager = datum.getInteger("nType") == 10;

            if (!isExists(roomId, contactId)) {
                RoomContact roomContact = new RoomContact()
                        .setId(idGenerator.next())
                        .setRoomId(roomId)
                        .setRoomSerialNo(datum.getString("vcChatRoomSerialNo"))
                        .setContactId(contactId)
                        .setContactSerialNo(datum.getString("vcManagerSerialNo"))
                        .setManager(isManager)
                        .setCreatedTime(now)
                        .setUpdatedTime(now);
                roomContactMapper.insert(roomContact);
            } else {
                LambdaUpdateWrapper<RoomContact> wrapper = new LambdaUpdateWrapper<RoomContact>()
                        .set(RoomContact::getManager, isManager)
                        .set(RoomContact::getUpdatedTime, now)
                        .eq(RoomContact::getRoomId, roomId)
                        .eq(RoomContact::getContactId, contactId);
                roomContactMapper.update(wrapper);
            }
        }
    }

    private boolean isExists(String roomId, String contactId) {
        LambdaQueryWrapper<RoomContact> wrapper = new LambdaQueryWrapper<RoomContact>()
                .eq(RoomContact::getRoomId, roomId)
                .eq(RoomContact::getContactId, contactId);
        return roomContactMapper.exists(wrapper);
    }
}
